<template>
  <div>
    <div class="header">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item>
          <el-input clearable v-model="form.keyword" placeholder="请输入关键词搜索" @keyup.enter="toSearch"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 第二行 -->
    <div class="table">
      <el-table :data="orderData" style="width: 100%" v-loading="loading">
        <el-table-column type="index" :index="indexMethod" label="序号" width="180" align="center"></el-table-column>
        <el-table-column prop="engineer_name" label="工程名称" width="180" align="center"></el-table-column>
        <el-table-column prop="device_name" label="设备名称" align="center"></el-table-column>
        <el-table-column prop="bill_why" label="问题描述" align="center"></el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template v-slot="scope">
            <el-tag :type="scope.row.status === '待接单' ? 'success' : (scope.row.status === '进行中' ? 'warning' : 'info')">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="工单类型" align="center">
          <template v-slot="scope">
            <span v-if="scope.row.type === 1">维修</span>
            <span v-else-if="scope.row.type === 2">安装</span>
            <span v-else>{{ scope.row.type }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <!-- 点击详情时，传递工单详情所需的所有字段 -->
            <el-button type="text" @click="handleDetail(scope.row)">详情</el-button>
            <el-button type="text" @click="handleCancel(scope.row)">取消</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page" style="margin-top: 10px;">
      <el-pagination hide-on-single-page @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="params.page" :page-sizes="[5, 8, 10, 20]" :page-size="params.pageSize"
        layout="total,sizes,prev,pager,next,jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { get } from '@/utils/request'
export default {
  data() {
    return {
      form: { keyword: '' },
      orderData: [],
      params: { page: 1, pageSize: 10 },
      total: 0,
      loading: false
    };
  },
  methods: {
    indexMethod(index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1;
    },
    handleSizeChange(val) {
      this.params.pageSize = val;
      this.params.page = 1;
      this.getOrdData();
    },
    handleCurrentChange(val) {
      this.params.page = val;
      this.getOrdData();
    },
    toSearch() {
      this.params.page = 1;
      this.getOrdData();
    },
    resetSearch() {
      this.form.keyword = '';
      this.params.page = 1;
      this.getOrdData();
    },
    async getOrdData() {
      this.loading = true;
      try {
        let temp = { page: this.params.page, pageSize: this.params.pageSize };
        if (this.form.keyword && this.form.keyword.trim()) {
          temp.keyword = this.form.keyword.trim();
        }
        let res = await get('/workorder/pageQuery', temp);
        let rawList = res && res.data ? (res.data.list || []) : [];
        if (this.form.keyword && this.form.keyword.trim()) {
          const keyword = this.form.keyword.trim();
          this.orderData = rawList.filter(item =>
            item.engineer_name && item.engineer_name.includes(keyword)
          );
          this.total = this.orderData.length;
        } else {
          this.orderData = rawList;
          this.total = res && res.data ? (res.data.total || 0) : 0;
        }
      } catch (error) {
        console.error('获取工单数据失败:', error);
        this.$message.error('获取数据失败');
        this.orderData = [];
        this.total = 0;
      } finally {
        this.loading = false;
      }
    },
    // 跳转详情页：传递完整工单数据，确保详情页能获取到serial_number等字段
    handleDetail(row) {
      this.$router.push({
        path: '/am/order/Details', // 与详情页路由路径保持一致
        query: { 
          id: row.id,
          serial_number: row.serial_number, // 工单编号
          type: row.type === 1 ? '维修' : row.type === 2 ? '安装' : row.type, // 工单类型（转文字）
          name: row.engineer_name, // 工单名称（复用工程名称字段）
          status: row.status // 工单状态
        }
      });
    },
    handleCancel(row) {
      this.$confirm('确定要取消该工单吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.orderData = this.orderData.filter(item => item.id !== row.id);
        this.$message.success('取消工单成功');
      }).catch(() => {
        this.$message.info('已取消操作');
      });
    }
  },
  created() {
  this.getOrdData();

  }
};
</script>
<style scoped>
.header{margin-bottom: 20px;}
</style>